package com.creative.xcx.controller;

import com.alibaba.excel.EasyExcel;
import com.creative.core.controller.BaseController;
import com.creative.core.domain.AjaxResult;
import com.creative.core.page.TableDataInfo;
import com.creative.xcx.domain.MaterialType;
import com.creative.xcx.service.MaterialTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author ling
 * @description 素材分类类型
 * @date 2025-02-26
 */
@RestController
@RequestMapping(value = "/MaterialType")
public class MaterialTypeController extends BaseController {

    @Resource
    private MaterialTypeService materialTypeService;

    /**
     * 新增
     *
     * @author ling
     * @date 2025/02/26
     **/
    @PostMapping
    public AjaxResult insert(@RequestBody MaterialType materialType) {
        return toAjax(materialTypeService.insert(materialType));
    }

    /**
     * 刪除单个
     *
     * @author ling
     * @date 2025/02/26
     **/
    @DeleteMapping("/{typeId}")
    public AjaxResult delete(@PathVariable("typeId") String typeId) {
        return toAjax(materialTypeService.delete(typeId));
    }

    /**
     * 批量刪除
     *
     * @author ling
     * @date 2025/02/26
     **/

    @DeleteMapping("/batch")
    public AjaxResult deleteBatch(@RequestBody String[] ids) {
        return toAjax(materialTypeService.deleteBatch(ids));
    }


    /**
     * 更新
     *
     * @author ling
     * @date 2025/02/26
     **/
    @PutMapping("/update")
    public AjaxResult update(@RequestBody MaterialType materialType) {
        return toAjax(materialTypeService.update(materialType));
    }


    /**
     * 查询 根据主键 id 查询
     *
     * @author ling
     * @date 2025/02/26
     **/
    @GetMapping(value = "/{typeId}")
    public AjaxResult selectById(@PathVariable("typeId") String typeId) {

        if (typeId == null) {
            return error("参数为空");
        }
        return success(materialTypeService.selectById(typeId.trim()));
    }

    /**
     * 查询 分页查询(带条件)
     *
     * @author ling
     * @date 2025/02/26
     **/
    @GetMapping("/list")
    public TableDataInfo selectMaterialTypeList(MaterialType materialType) {

        return materialTypeService.selectMaterialTypeList(materialType);
    }

    /**
     * 获取小程序分类
     *
     * @param materialType 对象
     * @return {@link TableDataInfo }
     * @author ling
     * @date 2025/02/27
     */
    @GetMapping("/mp/list")
    public AjaxResult selectMpMaterialTypeList(MaterialType materialType) {
        return success(materialTypeService.selectMpMaterialTypeList(materialType));
    }

    /**
     * 批量导出
     *
     * @author ling
     * @date 2025/02/26
     **/
    @PostMapping("/export")
    public void export(@RequestBody String[] ids, HttpServletResponse response) throws IOException {
        //        判断ids是否为空
        if (ids == null || ids.length == 0) {
            // ids 为空或未初始化时执行的代码
            throw new RuntimeException("ids为空");
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("素材分类类型" + System.currentTimeMillis(), StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), MaterialType.class)
                .sheet("素材分类类型")
                .doWrite(() -> {
                    // 查询数据
                    return materialTypeService.exportBatch(ids);
                });
        logger.info("导出成功");
    }

    /**
     * 获取素材类型列表
     *
     * @return {@link AjaxResult }
     * @author ling
     * @date 2025/03/06
     */
    @GetMapping("/type")
    public AjaxResult selectTypeList() {
        return success(materialTypeService.selectTypeList());
    }
}
